package com.example.restatr.Atcivity.GarbageClassification;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GarbageInfoAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private TextView introduce;
    private TextView guide;
    private RecyclerView rv;
    private GarbageClassBean.RowsBean data;
    private List<String> names = new ArrayList<>();
    private List<String> imgUrls = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_garbage_info_at);

        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        introduce = findViewById(R.id.introduce);
        guide = findViewById(R.id.guide);
        rv = findViewById(R.id.rv);
        data = new Gson().fromJson(getIntent().getStringExtra("data"), new TypeToken<GarbageClassBean.RowsBean>() {
        }.getType());
        Tools.SetBarTitle(toolbar, barTitle, data.getName(), search, this, false);
        introduce.setText(data.getIntroduce());
        guide.setText(data.getGuide());
        rv.setLayoutManager(new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false));
        GetGarbageExample();
    }

    private void GetGarbageExample() {
        new Thread(() -> {
            try {
                String token = Tools.Get("/prod-api/api/garbage-classification/garbage-example/list?type=" + data.getId(), MainActivity.sp.getString("token", ""));
                JSONObject jx = new JSONObject(token);
                JSONArray rows = jx.getJSONArray("rows");
                for (int i = 0; i < rows.length(); i++) {
                    JSONObject all = rows.getJSONObject(i);
                    names.add(all.getString("name"));
                    imgUrls.add(all.getString("imgUrl"));
                }
                runOnUiThread(()-> rv.setAdapter(new adapter()));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }).start();
    }

    class adapter extends RecyclerView.Adapter<adapter.viewHolder> {

        @NonNull
        @Override
        public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.garbage_type_item, parent, false);
            return new viewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull viewHolder holder, int position) {
            Glide.with(GarbageInfoAt.this)
                    .load(Tools.ip + imgUrls.get(position))
                    .into(holder.img);
            holder.name.setText(names.get(position));
        }

        @Override
        public int getItemCount() {
            return names.size();
        }

        class viewHolder extends RecyclerView.ViewHolder {
            private ImageView img;
            private TextView name;

            public viewHolder(@NonNull View itemView) {
                super(itemView);
                img = itemView.findViewById(R.id.img);
                name = itemView.findViewById(R.id.name);

            }
        }
    }

}